package com.haitonghop.main;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.haitonghop.R;
import com.haitonghop.data.GreatAppItem;
import com.haitonghop.network.APIRequestWorker;
import com.haitonghop.network.Request;
import com.haitonghop.network.RequestListener;
import com.haitonghop.requester.GreatAppRequester;

public class GreatAppActivity extends Activity implements RequestListener {

	LinearLayout loadingPage = null;
	LinearLayout loadingError = null;
	LinearLayout mainContent = null;
	ListView imageListView = null;
	GreatAppRender greatAppRender = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.great_app_act);

		loadingPage = (LinearLayout) findViewById(R.id.loadingpagetamdiem);
		loadingError = (LinearLayout) findViewById(R.id.loadingError);
		mainContent = (LinearLayout) findViewById(R.id.mainContent);
		imageListView = (ListView) findViewById(R.id.mainList);
		LinearLayout introApp = (LinearLayout)findViewById(R.id.introApp);
		introApp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://haibl.net/site/contact.html"));
				startActivity(browserIntent);

			}
		});

		loadingError.setVisibility(View.INVISIBLE);

		imageListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
				GreatAppItem item = appItems.get(index);
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.link));
				startActivity(browserIntent);
			}
		});
	}

	boolean isStart = true;
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if(isStart){
			isStart = false;

			GreatAppRequester appRequester = new GreatAppRequester(this);
			APIRequestWorker.getInstance().addAPIRequest(appRequester);
		}
	}

	ArrayList<GreatAppItem> appItems;
	void fillDataToList(ArrayList<GreatAppItem> appItems){
		this.appItems = appItems;
		if(greatAppRender == null){
			greatAppRender = new GreatAppRender(this, -1, -1, appItems);
		}

		imageListView.setAdapter(greatAppRender);
		greatAppRender.notifyDataSetChanged();
	}

	@Override
	public void onRequestComplete(final Request request) {
		// TODO Auto-generated method stub
		if(request != null){
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					loadingError.setVisibility(View.GONE);
					loadingPage.setVisibility(View.GONE);
					imageListView.setVisibility(View.VISIBLE);


					GreatAppRequester appRequester = (GreatAppRequester)request;
					fillDataToList(appRequester.getData());
				}
			});
		}
	}
	@Override
	public void onRequestFailed(String errorMsg, Request request) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				loadingPage.setVisibility(View.GONE);
				loadingError.setVisibility(View.VISIBLE);
			}
		});

	}


}
